<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/21
 * Time: 19:47
 */

class Grab extends CI_Controller
{

    private $base_url = "http://ah.huatu.com/zt/";


    public function __construct()
    {
        parent::__construct();
        error_reporting(E_ALL & ~E_NOTICE);
        $this->load->model('grabs');
    }

    /**
     * 获取首页内容 城市链接
     */
    public function index()
    {
        set_time_limit(3600);
        $HomeContent = $this->_remote_curl($this->base_url . 'jszwb/jsfsx/');
        $pattern = '/<a target="_blank" href="(?P<link_url>(.*?)\d+(.*?))">(?P<city_name>.*?)<\/a>/';
        preg_match_all($pattern, $HomeContent, $matchs);
        if (!isset($matchs['link_url'])) {
            show_error('抓取城市链接地址失败！');
        }
        $this->_getCityContent($matchs['link_url']);
    }


    /**获取城市内容 县 区 链接
     * @param $cityUrl
     */
    private function _getCityContent($cityUrl)
    {
        foreach ($cityUrl as $url) {
            $CityContent = $this->_remote_curl($url);
            $pattern = '/<a href=\'\.\.(?P<link_url>.*?)\'>(?P<area_name>.*?)<\/a>/';
            preg_match_all($pattern, $CityContent, $matchs);
            if (!isset($matchs['link_url'])) {
                show_error('抓取县、区链接地址失败！');
            }
            $this->_getAreaContent($matchs['link_url']);
        }

    }


    /**获取区、县内容 职位详情链接
     * @param $areaUrl
     */
    private function _getAreaContent($areaUrl)
    {
        foreach ($areaUrl as $url) {
            $url = $this->base_url . 'jscj/' . $url;
            $AreaContent = $this->_remote_curl($url);
            $pattern = '/<a href=\'\.\.\/\.\.(?P<link_url>.*?)\' class=\'num table3\'>.*?<\/a>/';
            preg_match_all($pattern, $AreaContent, $matchs);
            if (!isset($matchs['link_url'])) {
                show_error('抓取职位链接地址失败！');
            }
            $this->_zhiWeiContent($matchs['link_url']);
        }
    }


    private function _zhiWeiContent($zhiWeiUrl)
    {

        foreach ($zhiWeiUrl as $url) {
            $url = $this->base_url . 'jscj/' . $url;
            $find = $this->grabs->find_grab_log($url);
            if($find){
                continue;
            }
            $DetailContent = $this->_remote_curl($url);
            $pattern = '/<tr>.*职位代码<\/td><td>(?P<code>\d+)<\/td><\/tr>/';
            preg_match_all($pattern, $DetailContent, $matchs);
            if (!isset($matchs['code'][0])) {
                continue;
            }
            $temp = array();
            $code = $matchs['code'][0];
            $temp['post_code'] = $code;
            //获取报名人数 、资格审查合格、缴费人数、竞争比
            $pattern = '/<tr style="background:#ececec;"><td>(?P<key>.*?)<\/td><td>(?P<value>(\d+\.\d+)|(\d+)).*<\/td><\/tr>/';
            preg_match_all($pattern, $DetailContent, $matchs);
            foreach ($matchs['key'] as $k => $name) {
                if ($name == '报名人数') {
                    $temp['apply_num'] = $matchs['value'][$k];
                }
                if ($name == '资格审查合格') {
                    $temp['pass_num'] = $matchs['value'][$k];
                }
                if ($name == '缴费人数') {
                    $temp['pay_num'] = $matchs['value'][$k];
                }
                if ($name == '竞争比') {
                    $temp['compete_rate'] = $matchs['value'][$k];
                }

            }
            if ($code) {
                $this->grabs->insert_grab_log($url);
                $this->grabs->update_post_table($temp);
            }
        }

    }


    private function _remote_curl($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 120);
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
        $result = curl_exec($ch);
        curl_close($ch);
        $result = mb_convert_encoding($result, 'utf-8', 'UTF-8,GBK,GB2312,BIG5');
        return $result;
    }

    public function grab_data(){
        $data = array();
        $row=0;
        for ($i=1;$i<=237;$i++){
            $content  = $this->_remote_curl("http://jszp.ahzsks.cn/findStatistic-0-0-0-{$i}.do");
            $pattern = "/<td align=\"(?P<keys>.*?)\" alt=\"(?P<key>.*?)\">/";
            $content = str_replace(array("\r\n","\r","\n"),"",$content);
            preg_match_all($pattern, $content, $matchs);
            foreach ($matchs[2] as $k=>$value){
                $pattern = '/^\d{12}/';
                preg_match_all($pattern, $value, $matchs2);

                if($matchs2[0][0]){
                    $row++;
//                    echo $row.'-'.$matchs2[0][0].'<br/>';
//                    echo ($k+3).'-'.$matchs[2][$k+3].'<br/>';
//                    echo ($k+4).'-'.$matchs[2][$k+4].'<br/>';
//                    echo ($k+5).'-'.$matchs[2][$k+5].'<br/>';
//                    echo ($k+6).'-'.$matchs[2][$k+6].'<br/>';
                    $temp = array();
                    $temp['post_code'] = $matchs2[0][0];
                    $temp['num'] = $matchs[2][$k+3];
                    $temp['apply_num'] = $matchs[2][$k+4];
                    $temp['pass_num'] = $matchs[2][$k+5];
                    $temp['pay_num'] = $matchs[2][$k+6];
                    $data[] = $temp;
                }
            }
        }
        $total = count($data);
        $success =$error=0 ;
        foreach($data as $k=>$value){
            $find = $this->grabs->find_one($data[$k]);
            if($find){
                unset($data[$k]['num']);
                $this->grabs->update_post_table($data[$k]);
                $success++;
            }else{
                $error++;
            }
        }
        echo '<div style="color: red;font-weight: bold">抓取成功总条数:'.$total.'</div>';
        echo '<div style="color: red;font-weight: bold">更新成功条数:'.$success.'</div>';
        echo '<div style="color: red;font-weight: bold">更新失败条数:'.$error.'</div>';

    }
}